import 'package:flutter/material.dart';
import 'package:message_tools/application.dart';
import 'package:message_tools/components/index.dart';
import 'package:message_tools/router/index.dart';
import 'package:message_tools/store/index.dart';
import 'package:message_tools/store/view_model/sms_template_model.dart';

class SmsTemplateListPage extends StatefulWidget {
  _SmsTemplateListPageState createState() => _SmsTemplateListPageState();
}

class _SmsTemplateListPageState extends State<SmsTemplateListPage> {
  @override
  Widget build(BuildContext context) {
    Store.value<SmsTemplateModel>(context).loadData();

    return Scaffold(
        appBar: AppBarPlugin.Create(
          "短信模板",
          centerTitle: true,
          leading: Builder(builder: (BuildContext c) {
            return IconButton(
                icon: const Icon(Icons.arrow_back),
                onPressed: () {
                  Navigator.pop(context);
                });
          }),
          actions: <Widget>[
            IconButton(
                icon: Image.asset('assets/images/add.png'),
                onPressed: () {
                  Application.setString("edit_sms_template", "");
                  Application.router.navigateTo(
                      context, Routes.RouteAddSmsTemplatePage,
                      transition: TransitionType.fadeIn);

                  /*Navigator.push(
                    context,
                    new MaterialPageRoute(
                        builder: (ctx) => AddSmsTemplatePage()),
                  );*/
                })
          ],
        ),
        body: Container(
            color: Colors.grey[100],
            child: Store.connect<SmsTemplateModel>(
                builder: (context, snapshot, child) {
              return _buildListViewItem(snapshot.list);
            })));
  }

  Widget _buildListViewItem(List list) {
    return ListView.builder(
      scrollDirection: Axis.vertical,
      itemCount: list.length, // item 的个数
      itemBuilder: (BuildContext context, int index) {
        var smsTemplate = list[index];
        return Container(
          margin: const EdgeInsets.all(5),
          padding: const EdgeInsets.all(10),
          decoration: new BoxDecoration(
            //背景
            color: Colors.white,
            //设置四周边框
            border: createBorder(color: Colors.grey[300]),
          ),
          child: Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  Text(smsTemplate.title,
                      style: TextStyle(
                          height: 1,
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                          color: Colors.grey[600])),
                ],
              ),
              Divider(
                height: 10.0,
                indent: 0.0,
                color: Colors.grey,
              ),
              Row(
                children: <Widget>[
                  LimitedBox(
                      maxWidth: MediaQuery.of(context).size.width - 32,
                      child: Text(
                        smsTemplate.content,
                        softWrap: true,
                        overflow: TextOverflow.ellipsis,
                        maxLines: 100,
                        textAlign: TextAlign.left,
                        style: new TextStyle(
                            height: 1,
                            fontSize: 16,
                            color: Colors.grey[700],
                            letterSpacing: 1.2),
                      ))
                ],
              ),
              Divider(
                height: 10.0,
                indent: 0.0,
                color: Colors.grey,
              ),
              Row(
                children: <Widget>[
                  MaterialButton(
                      child: Text("修改"),
                      onPressed: () {
                        Application.setString("edit_sms_template",
                            Application.jsonEncode((smsTemplate)));

                        Application.router.navigateTo(
                            context, Routes.RouteAddSmsTemplatePage,
                            transition: TransitionType.fadeIn);
                      }),
                  MaterialButton(child: Text("删除"), onPressed: () {})
                ],
              ),
            ],
          ),
        );
      },
    );
  }
}

/// ListView 默认的实例,有状态
